import java.io.File;
import java.io.IOException;
import java.util.Date;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;


public class Searcher {
	public static void Search(String szPath, String szQueried)
	{
		File file = new File(szPath);
		if (file.exists() && file.canRead() && file.isDirectory())
			Search(file, szQueried);
		else
			System.out.println("Can not find Path " + szPath);
	}
	
	public static void Search(File indexDir, String szQueried)
	{
		long nStart = new Date().getTime();
		long nEnd = 0;
		
		try {
			Directory dir = FSDirectory.getDirectory(indexDir, false);
			IndexSearcher is = new IndexSearcher(dir);
			QueryParser parser = new QueryParser("contents", new StandardAnalyzer());
			Query query = parser.parse(szQueried);
			Hits hits = null;
			hits = is.search(query);
			nEnd = new Date().getTime();	
			System.out.println("\nSearing " + szQueried + " in index takes " + (nEnd - nStart) + " millionSecond");
			System.out.println("Get " + hits.length() + " relavent files.");
			for (int i = 0; i < hits.length(); i++)
			{
				System.out.println(hits.doc(i).get("path") + "    score: " + hits.score(i));
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
}
